export default{
	hideLoading() {
		uni.hideLoading();
	},
	showLoadingNotHidden(text) {
		uni.showLoading({
			title: text
		});
	},
	showLoading(text) {
		uni.showLoading({
			title: text
		});
		
		getApp().globalData._showTimer && clearTimeout(getApp().globalData._showTimer);
		getApp().globalData._showTimer = setTimeout(() => {
			this.hideLoading();
		}, 3000)
	},

	showSuccess(msg){
		uni.showToast({
			title: msg
		})
	},
	showWaring(msg){
		uni.showToast({
			title: msg
		})
	},
	showError(msg){
		uni.showToast({
			title: msg
		})
	},
	setCache(key,v){
		uni.setStorageSync(key,v);
	},
	getCache(key){
		return uni.getStorageSync(key)||'';
	},
	toLogin(){
		let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
		let curRoute = routes[routes.length - 1].route // 获取当前页面路由，也就是最后一个打开的页面路由
		uni.redirectTo({
			url: '/pages/common/login?curRoute='+curRoute
		});
		
	},
	getNotLoading(url,param,success){
		let token = this.getCache('token')
		let path = getApp().globalData.doman+url
		uni.request({
			url: path, //仅为示例，并非真实接口地址。
			method: 'GET',
			header: {
				'token': token //自定义请求头信息
			},
			data: param,
			success: (res) => {
				this.hideLoading()
				let code = res.data.code
				
				// 未登录
				if (code == 1003){
					this.toLogin()
					return
				}
				success(res.data);
			},fail: (res) => {
				this.showError('请求出错')
				console.info(res)
				this.hideLoading()
			}
		});
	},
	get(url,param,success){
		this.showLoadingNotHidden('请求中......')
		let token = this.getCache('token')
		let path = getApp().globalData.doman+url
		uni.request({
			url: path, //仅为示例，并非真实接口地址。
			method: 'GET',
			header: {
				'token': token //自定义请求头信息
			},
			data: param,
			success: (res) => {
				this.hideLoading()
				let code = res.data.code
				
				// 未登录
				if (code == 1003){
					this.toLogin()
					return
				}
				success(res.data);
			},fail: (res) => {
				this.showError('请求出错')
				console.info(res)
				this.hideLoading()
			}
		});
	},
	post(url,param,success){
		this.showLoadingNotHidden('请求中......')
		let token = this.getCache('token')
		let path = getApp().globalData.doman+url
		uni.request({
			url: path, //仅为示例，并非真实接口地址。
			method:'POST',
			header: {
				'token': token //自定义请求头信息
			},
			data: param,
			success: (res) => {
				this.hideLoading()
				let code = res.data.code
				// 未登录
				if (code == 1003){
					this.toLogin()
					return
				}
				success(res.data);
			},fail: (res) => {
				this.showError('请求出错')
				console.info(res)
				this.hideLoading()
			}
		});
	},
	upload(path,success){
		this.showLoadingNotHidden('请求中......')
		let token = this.getCache('token')
		let url = getApp().globalData.doman+'/api/file/upload'
		uni.uploadFile({
			url: url, // 你的服务器上传接口地址
			filePath: path, // 选择的图片路径
			name: 'file', // 必填，后台用来解析的文件名
			header: {
				'token': token //自定义请求头信息
			},
			success: (res) => {
				this.hideLoading()
				let code = res.data.code
				// 未登录
				if (code == 1003){
					this.toLogin()
					return
				}
				success(JSON.parse(res.data));
			},
			fail: (err) => {
				// 上传失败的处理...
				this.hideLoading()
				this.showError('上传失败')
				console.error("上传失败的处理...",err);
				
			}
		});
	},
	timestampToTime: function (timestamp) {
	  if(timestamp=='' || timestamp == null || timestamp ==undefined){
		return ''
	  }
	  var date = new Date(timestamp)
	  var Y = date.getFullYear()
	  var M = (date.getMonth() + 1)
	  var D = date.getDate()
	  var h = date.getHours()
	  var m = date.getMinutes()
	  var s = date.getSeconds()
	  let M1 = M < 10 ? '0' + M : M
	  let D1 = D < 10 ? '0' + D : D
	  return Y +'-'+ M1 +'-'+ D1
	},
	timestampToTimeMils: function (timestamp) {
	  if(timestamp=='' || timestamp == null || timestamp ==undefined){
		return ''
	  }
	  var date = new Date(timestamp)
	  var Y = date.getFullYear()
	  var M = (date.getMonth() + 1)
	  var D = date.getDate()
	  var h = date.getHours()
	  var m = date.getMinutes()
	  var s = date.getSeconds()
	  let M1 = M < 10 ? '0' + M : M
	  let D1 = D < 10 ? '0' + D : D
	  let h1 = h < 10 ? '0' + h : h
	  let m1 = m < 10 ? '0' + m : m
	  let s1 = s < 10 ? '0' + s : s
	  return Y +'-'+ M1 +'-'+ D1 +' '+ h1 +':'+ m1 +':'+ s1
	},
	navback(){
		uni.navigateBack()
	},
	_confirm(msg,success){
		uni.showModal({
		  title: '操作确认',
		  content: msg,
		  confirmColor: '#0081ff',  // 确认按钮颜色
		  cancelColor: '#39B54A',   // 取消按钮颜色
		  success: (res) => {
		    if(res.confirm) {
		      success()
		      // 确认后逻辑
		    } else {
		      // console.log('用户取消操作');
		    }
		  }
		})
	}
}
